﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
////using System.Linq;
using System.Text;
using System.Windows.Forms;
using Zzva.DomainObjectReg;
using Zzva.Common;
using Zzva.DomainObject;
using System.Collections.ObjectModel;
using Zzva.Report;
using Zzva.ReportExcel;
using System.Collections; 

namespace Zzva.UIWin
{
    public partial class TFrmRegDirITCompany : TFrmRegDirBase 
    {
        TRegDirITCompany mvarRegDirITCompany;



        protected override void FilterByValue()
        {
            try
            {
                TFrmFilterByValue FrmFilterByValue;

                ////FrmFilterByValue = new TFrmFilterByValue((TRegDirBase)mvarRegDirITCompany, GetListAliasField());
                FrmFilterByValue = new TFrmFilterByValue((TRegDirBase)mvarRegDirITCompany, GetListAliasField(),GetCurColumnObozn());


                FrmFilterByValue.EventReady += new EventHandler(FrmFilterByValue_Ready);
                FrmFilterByValue.ShowDialog();
            }

            catch (Exception e1) { throw e1; }
            finally { }
        }






























































































        protected override void Report()
        {
            try
            {

                TReportBase lReportBase;

                //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                //Здесь достаемимя класса Отчета из установок
                //и по технологии Позднего связывания 
                //создаем объект Отчет
                //А пока так

                lReportBase = new TReportExcel();

                ////lReportBase.Title = mvarRegDirITCompany.Title;
                lReportBase.Title = mvarRegDirITCompany.TitleFull;

                object lList = mvarRegDirITCompany.GetCurListView();

                //lReportBase.CreateReport(lList);
                //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                //Здесь достаем парметр из установок:
                //Как мы хтим видеть поля отчета, представляющие объекты:
                //развернутыми по всем полям, или только строковое представление
                //А пока так по  умолчанию
                lReportBase.CreateReport(lList, EReportFieldView.ExpandAsObject);

            }
            catch (Exception e1) { MessageBox.Show(e1.Message); }
            finally { }
        }

        protected override void ViewCurrentList()
        {
            try
            {
                object hhh = mvarRegDirITCompany.GetCurListView();

                bool lFlgStateLoadForm;
                lFlgStateLoadForm = mvarFlgStateLoadForm;
                if (mvarFlgStateLoadForm == false) { mvarFlgStateLoadForm = true; }

                dgvMain.DataSource = null;
                dgvMain.Rows.Clear();
                dgvMain.DataSource = hhh;

                DataGridViewColumn lDataGridViewColumn = dgvMain.Columns["Id"];
                lDataGridViewColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
                lDataGridViewColumn.Resizable = DataGridViewTriState.False;
                //lDataGridViewColumn.ReadOnly = true;
                lDataGridViewColumn.Width = 0;

                //lDataGridViewColumn.Visible = false;

                mvarFlgStateLoadForm = lFlgStateLoadForm;

                TDirITCompany lCurItem;

                lCurItem = (TDirITCompany)mvarRegDirITCompany.GetCurItem();

                int lCurIndex;

                if (lCurItem != null)
                {
                    lCurIndex = GetIndexForId(dgvMain.Rows, lCurItem.Id);

                    if (lCurIndex == -1) { throw (new CommonException("Текущая запись не найдена")); }
                    //dgvMain.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                    //dgvMain.MultiSelect = false;
                    //dgvMain.Rows[lCurIndex].Selected = true; 

                    //dgvMain.CurrentCell = dgvMain.Rows[lCurIndex].Cells[1];
                    dgvMain.CurrentCell = dgvMain.Rows[lCurIndex].Cells[mvarFlgColumnIndexCur];
                }


                ////
                this.Text = mvarRegDirITCompany.TitleFull;


            }
            catch (Exception e) { throw e; }
            finally { }
        }



        #region(constructor)
        public TFrmRegDirITCompany(TRegDirITCompany pRegDirITCompany)
            : base()
        {
            InitializeComponent();
            mvarRegDirITCompany = pRegDirITCompany;
            ViewCurrentList();



            if (mvarRegDirITCompany.CheckEventUpdateListIsNull() == true)
            {
                mvarRegDirITCompany.EventUpdateList += new EventHandler(RegDirBase_UpdateList);
            }


            if (mvarRegDirITCompany.CheckEventFindDirIsNull() == true)
            {
                mvarRegDirITCompany.EventFindDir += new EventHandler(RegDirBase_FindDir);
            }



        }
        #endregion




























































































































































////        protected override void SetCurItem(int pId)
////        {
////            ////try{mvarRegDirITCompany.SetCurItem(pId);}
////            try { mvarRegDirITCompany.SetCurItem(pId); }
////            catch (Exception e1) { throw e1; }
////            finally { }
////        }

        protected override void SetCurItem(int pCurItem)
        {
            try { mvarRegDirITCompany.SetCurItem(pCurItem); }
            catch (Exception e1) { throw e1; }
            finally { }
        }









////       private void FrmDirITCompany_Ready(object Sender, EventArgs ArgsBase)
////       {
////           try
////           {
////               int lId;
////               TDirITCompanyNew lDir;
////               TDirEventArgs lDirEventArgs;
////
////                lDirEventArgs = (TDirEventArgs)ArgsBase;
////                lId = lDirEventArgs.Id;
////                lDir = (TDirITCompanyNew)lDirEventArgs.Dir;
////
////                if (lId == 0) { AddDir(lDir); }
////                else { EditDir(lDir); }
////            }
////            catch (Exception e) { throw e; }
////            finally { }
////        }


        protected override  void FrmDirBase_Ready(object Sender, EventArgs ArgsBase)
        {
            try
            {
                int lId;
                TDirITCompanyNew lDir;
                TDirEventArgs lDirEventArgs;

                lDirEventArgs = (TDirEventArgs)ArgsBase;
                lId = lDirEventArgs.Id;
                lDir = (TDirITCompanyNew)lDirEventArgs.Dir;

                if (lId == 0) { AddDir(lDir); }
                else { EditDir(lDir); }
            }
            catch (Exception e) { throw e; }
            finally { }
        }








////        private void RegDirBase_FindDir(object lSender, EventArgs lEventArgs)
////        {
////            {
////                try
////                {
////                    bool lResult;
////                    TDirBase lCurDir;
////                    int lCurIndex;
////
////                    TDirFindEventArgs lDirFindEventArgs = (TDirFindEventArgs)lEventArgs;
////
////                    lResult = lDirFindEventArgs.Result;
////                    lCurDir = lDirFindEventArgs.CurDir;
////
////                    if (lResult == false)
////                    {
////                        ////throw (new CommonException("Ничего не найдено"));
////                    }
////                    else
////                    {
////                        lCurIndex = GetIndexForId(dgvMain.Rows, lCurDir.Id);
////                        if (lCurIndex == -1) { throw (new CommonException("Текущая запись не найдена")); }
////                        dgvMain.CurrentCell = dgvMain.Rows[lCurIndex].Cells[mvarFlgColumnIndexCur];
////                    }
////                }
////
////                catch (Exception e1) { throw e1; }
////                finally { }
////            }
////        }




 ////       private void RegDirBase_UpdateList(object lSender, EventArgs lEventArgs)
 ////       {
 ////           {
 ////               try
 ////               {
 ////                   ViewCurrentList();
 ////               }
 ////               catch (Exception e1) { throw e1; }
 ////               finally { }
 ////           }
 ////       }



 


////         //Возвратить индекс строки  ДатаГрид по ИД  
////        private int GetIndexForId(DataGridViewRowCollection lGridViewRowCollection, int pId)
////         {
////             try
////             {
////                 int lIndex;
////                 lIndex = -1;
//// 
////                 foreach (DataGridViewRow dr in lGridViewRowCollection)
////                 {
////                     if ((int)(dr.Cells["Id"].Value) == pId)
////                     {
////                         lIndex = dr.Index;
////                         break;
////                     }
////                 }
//// 
////                 return lIndex;
////             }
//// 
////             catch (Exception e1) { throw e1; }
////             finally { }
////         }





















        #region Find...




        //Установить курсор на заданном объекте
        public override  void FindByDir(TDirBase pDir)
        {
            try
            {
                mvarRegDirITCompany.FindByDir(pDir);  
            }

            catch (Exception e1) { MessageBox.Show(e1.Message); }
            finally { }
        }






        protected override void FindPrev(TFilterValue pFindValue)
        {
            try
            {
                ////mvarRegDirITCompany.FindPrev(pFindValue);
                mvarRegDirITCompany.FindPrev(pFindValue);

            }
            catch (Exception e1) { throw e1; }
            finally { }
        }


        protected override void FindNext(TFilterValue pFindValue)
        {
            try
            {
                ////mvarRegDirITCompany.FindNext(pFindValue);
                mvarRegDirITCompany.FindNext(pFindValue);

            }
            catch (Exception e1) { throw e1; }
            finally { }
        }



        protected override void Find(TFilterValue pFindValue)
        {
            try
            {
                ////mvarRegDirITCompany.Find(pFindValue);
                mvarRegDirITCompany.Find(pFindValue);
            }
            catch (Exception e1) { throw e1; }
            finally { }
        }






        #endregion



        #region Filter...

        protected override void FilterAdd(TFilterValue pFilterValue)
        {
            try
            {
                ////mvarRegDirITCompany.FilterAdd(pFilterValue);
                mvarRegDirITCompany.FilterAdd(pFilterValue);
            
            }
            catch (Exception e1) { throw e1; }
            finally { }
        }

        protected override void FilterDel()
        {
            try
            {
                ////mvarRegDirITCompany.FilterDel();
                mvarRegDirITCompany.FilterDel();

            }
            catch (Exception e1) { throw e1; }
            finally { }
        }

        #endregion


        protected override void Sort(TSortValue pSortValue)
        {
            try
            {
                ////mvarRegDirITCompany.Sort(pSortValue);
                mvarRegDirITCompany.Sort(pSortValue);

            }

            catch (Exception e1) { throw e1; }
            finally { }
        }


        protected override void Delete()
        {
            try
            {
                ////mvarRegDirITCompany.Delete();
                mvarRegDirITCompany.Delete();

            }

            catch (Exception e1) { throw e1; }
            finally { }
        }

        #region(Edit)


        private void EditDir(TDirITCompanyNew pDir)
        {
            try
            {
                ////mvarRegDirITCompany.Edit(pDir);
                mvarRegDirITCompany.Edit(pDir);

            }

            catch (Exception e1) { throw e1; }
            finally { }
        }


        protected override void Edit()
        {
            try
            {
                TFrmDirITCompany FrmDirITCompany;

                ////TDirITCompany lCurDir = mvarRegDirITCompany.GetCurItem();
                TDirITCompany lCurDir = (TDirITCompany)mvarRegDirITCompany.GetCurItem();


                FrmDirITCompany = new TFrmDirITCompany(lCurDir);
                
                ////FrmDirITCompany.EventReady += new EventHandler(FrmDirITCompany_Ready);
                FrmDirITCompany.EventReady += new EventHandler(FrmDirBase_Ready);


                ////FrmDirITCompany.Show();
                FrmDirITCompany.ShowDialog(); 




                //frmSub1.Visible = true;
                //frmSub1.ShowDialog(); 

            }

            catch (Exception e1) { throw e1; }
            finally { }
        }


        #endregion


  





        protected    void AddDir(TDirITCompanyNew pDir)
        {
            try
            {
                ////mvarRegDirITCompany.Add(pDir);
                mvarRegDirITCompany.Add(pDir);
            }
            catch (Exception e1) { throw e1; }
            finally { }
        }


        protected override void Ready()
        {
            try
            {
                ////TDirITCompany lDirITCompany = mvarRegDirITCompany.GetCurItem();
                TDirITCompany lDirITCompany = (TDirITCompany)mvarRegDirITCompany.GetCurItem();
                

                TDirEventArgs lDirEventArgs;
                lDirEventArgs = new TDirEventArgs(lDirITCompany, lDirITCompany.Id);
                FireEventReady(lDirEventArgs);
                this.Close();
            }

            catch (Exception e){MessageBox.Show(e.Message);}
            finally { }
        }






























 









 












 





























 



 









        #region(Add)





        protected override void Add()
        {
            try
            {
                TFrmDirITCompany FrmDirITCompany;
                FrmDirITCompany = new TFrmDirITCompany();

                ////FrmDirITCompany.EventReady += new EventHandler(FrmDirITCompany_Ready);
                FrmDirITCompany.EventReady += new EventHandler(FrmDirBase_Ready);



                ////FrmDirITCompany.Show();
                FrmDirITCompany.ShowDialog(); 



                //frmSub1.Visible = true;
                //frmSub1.ShowDialog(); 
            }

            catch (Exception e1) { throw e1; }
            finally { }
        }


 

        #endregion



 

  

 







 







 







    }
}
